#
# Copyright (C) 2015-2022 Lixiang Group Holding Limited
#
"""
cantrcv1145_ib_bswm更新处理
"""

from basic_func_module.autosar_utils.utils import get_definition_ref, get_value, get_short_name
from func_module.module_management.arxml_handler.ib_bswmd_process import ib_bswmd_utils


def update_ib_bswmd(internal_behavior_dict, ecuc_autosar_dict, asw_autosar_dict, system_autosar_dict, other_input_info):
    cantrcv1145_ib = internal_behavior_dict.get("CanTrcv1145", None)
    if cantrcv1145_ib is None:
        return

    cantrcv1145_autosar = ecuc_autosar_dict.get("CanTrcv1145", None)
    if cantrcv1145_autosar is None:
        return

    period = 0.01
    cantrcv1145_config = cantrcv1145_autosar.find("/ActiveEcuC/CanTrcv1145/CanTrcvGeneral")
    value_list = ib_bswmd_utils.get_parameter_values(cantrcv1145_config)
    for value_node in value_list:
        if get_definition_ref(value_node).split("/")[-1] != "CanTrcvMainFunctionPeriod":
            continue
        period = get_value(value_node)
        break

    timing_event_list = cantrcv1145_ib.find_nodes("BSW-TIMING-EVENT")
    for timing_event in timing_event_list:
        short_name = get_short_name(timing_event)
        if short_name != "CanTrcv_Tja1145_MainFunctionEvent0":
            continue
        timing_event.PERIOD.valueOf_ = str(period)
        break
